{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 条件判断\n",
    "\n",
    "#### `if`关键字\n",
    "Julia中，这样的语法\n",
    "\n",
    "```julia\n",
    "if *condition 1*\n",
    "    *option 1*\n",
    "elseif *condition 2*\n",
    "    *option 2*\n",
    "else\n",
    "    *option 3*\n",
    "end\n",
    "```\n",
    "\n",
    "让我们按照条件来执行不同的语句。\n",
    "<br><br>\n",
    "举个例子，我们也许想要实现FizzBuzz测试：给定一个数字N，如果N可以被3整除则打印\"Fizz\"，如果N可以被5整除则打印\"Buzz\"，如果N可以同时被3和5整除则打印\"FizzBuzz\"，其他情况则打印这个数字本身！在下面给N取一个值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "N = "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if (N % 3 == 0) && (N % 5 == 0) # `&&`指的是\"AND\"； % 是取余运算\n",
    "    println(\"FizzBuzz\")\n",
    "elseif N % 3 == 0\n",
    "    println(\"Fizz\")\n",
    "elseif N % 5 == 0\n",
    "    println(\"Buzz\")\n",
    "else\n",
    "    println(N)\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 三元运算符\n",
    "\n",
    "我们可以用三元运算符语法重写上一个cell\n",
    "\n",
    "```julia\n",
    "a ? b : c\n",
    "```\n",
    "\n",
    "它等价于\n",
    "\n",
    "```julia\n",
    "if a\n",
    "    b\n",
    "else\n",
    "    c\n",
    "end\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在来试试返回两个数中较大的数。在下面给`x`和`y`赋值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x =\n",
    "y ="
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用`if`和`else`关键字的话可以这么写："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if x > y\n",
    "    x\n",
    "else\n",
    "    y\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用三元运算符的话这个条件判断语句应该像这样："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(x > y) ? x : y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 短路运算\n",
    "\n",
    "我们已经见过这样的语法\n",
    "```julia\n",
    "a && b\n",
    "```\n",
    "当`a`和`b`都为真的时候返回真。当然了，如果`a`是假的话，Julia不需要判断`b`的真假就可以判断整体为假；这就“短路”了，直接返回`false`。第二个参数`b`可能是个复杂的表达式，比如说一个有副作用的函数调用，这样的话这个函数就不会被调用："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "false && (println(\"hi\"); true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "true && (println(\"hi\"); true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "另一方面，如果`a`是真，Julia只要返回`b`的值作为整个表达式的值。这就意味着不需要判断`b`的真假！`b`甚至可以是个报错："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(x > 0) && error(\"x cannot be greater than 0\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "类似的，检验一下逻辑“或”的`||`运算符，它也有短路运算。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "true || println(\"hi\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以及"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "false || println(\"hi\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 练习\n",
    "\n",
    "#### 5.1\n",
    "写一个条件判断语句：当一个数为偶数时打印自身，为计数时打印\"odd\"。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5.2\n",
    "将5.1中的代码用三元运算符重写。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.0.5",
   "language": "julia",
   "name": "julia-1.0"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.0.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
